﻿using Management.Auxiliary;
using Management.Core.Bouns;
using Management.Core.Cash;
using Management.Core.Introducer;
using Management.Core.Message;
using Management.Core.Users;
using System;
using System.Collections;
using System.Web.Mvc;

namespace Management.Controllers
{
    public class ZoneController : Controller
    {

        private IntroducerManager introducerManager = new IntroducerManager();
        private MessageManager messageManager = new MessageManager();
        private Management.Core.Users.UserManager userManager = new Management.Core.Users.UserManager();
        //
        // GET: /Zone/
        public ActionResult Index()
        {
            var IsMobile = Request.Browser.IsMobileDevice;
            if (IsMobile == false)
            {
                return View();
            }
            else
            {
                return View("~/Views/Mobile/Zone/VIP.cshtml");

            }
        }

        public ActionResult MessageList()
        {
            return Json(messageManager.FindList(10, int.Parse(Request.Cookies["UserInfo"]["UserID"]), int.Parse(Request.Cookies["UserInfo"]["UserRole"]), null), JsonRequestBehavior.AllowGet);
        }

        //
        [HttpPost]
        public ActionResult AddCash(int id)
        {
            CashManager cashManager = new CashManager();
            UserManager userManager = new UserManager();
            BounsManager bounsManager = new BounsManager();
            if (userManager.Find(id).Balance < decimal.Parse(Request["money"]))
            {
                return Json("提现金额不能超过账户可提现金额");
            }
            if (cashManager.Count(u => u.UserID == id && u.Static == 0) < 1)
            {
                Cash cash = new Cash();
                cash.CashType = 0;
                cash.Money = decimal.Parse(Request["money"]);
                cash.Static = 0;
                cash.UserID = id;
                cash.BounsID = bounsManager.Find(u => u.Floor == 1).BounsID;//这里需要注意的是分红比例表的第一层一定要设定比例，且比例必须为0
                cash.Created_at = DateTime.Now;
                cash.Updated_at = DateTime.Now;
                try
                {
                    cashManager.Add(cash);
                }
                catch (Exception ex)
                {
                    return Json(ex.Message);
                }
                return Json("申请成功，我们会在1~2天内站内信，短信，或电话通知方式回复您，敬请关注！");
            }
            else
                return Json("亲，您已经提交过提现申请了，请在上次申请通过后，再进行新的提现！");
        }

        [HttpPost]
        public ActionResult AddBouns(int id)
        {
            CashManager cashManager = new CashManager();
            UserManager userManager = new UserManager();
            int floor = int.Parse(Request["floor"]);
            if (cashManager.Find(u => u.UserID == id && u.Floor == floor) != null)
            {
                return Json("您已经获得了该层所发的分红，请继续加油！");
            }
            else
            {
                Cash cash = new Cash();
                User user = userManager.Find(id);
                BounsManager bounsManager = new BounsManager();
                if (bounsManager.Find(u => u.Floor == floor) == null)
                {
                    return Json("您达到的层级太深了，我们的分红奖励还没到这么深，敬请期待！");
                }
                double per = bounsManager.Find(u => u.Floor == floor).Per;

                cash.Floor = floor;
                cash.CashType = 1;
                cash.Created_at = DateTime.Now;
                cash.Updated_at = DateTime.Now;
                cash.Money = user.Balance * (decimal)per / 100;
                cash.UserID = id;
                cash.BounsID = bounsManager.Find(u => u.Floor == floor).BounsID;
                try
                {
                    //userManager.Update(user);
                    cashManager.Add(cash);
                }
                catch (Exception ex)
                {
                    return Json(ex.Message);
                }
                return Json("您的分红申请已提交，我们会尽快处理，请关注你的余额变化。");

            }
        }


        [HttpPost]
        public ActionResult GetTree(int userid)
        {
            //   int userid = int.Parse(Request["userid"]);
            Introducer _introducer = new Introducer();
            _introducer = introducerManager.Find(u => u.UserID == userid);
            //_introducer = introducerManager.FindList().OrderBy(u => u.ID).First();//客户要求，返回所有二叉树，所以从定点开始生成树
            ArrayList hs = new ArrayList();
            return Json(GetTree2(_introducer, hs));
        }

        //将树形结构以层次遍历的方式输出
        private ArrayList GetTree2(Introducer _introducer, ArrayList nodelist)
        {
            ArrayList Nodes = new ArrayList();
            int front = -1;
            int near = -1;
            if (_introducer != null)
            {
                near++;
                //nodelist.Add(_introducer);
                Nodes.Add(_introducer);
            }
            while (front != near)
            {
                front++;
                nodelist.Add(Nodes[front]);
                _introducer = (Introducer)Nodes[front];
                if (_introducer.LeftID > 0)
                {
                    near++;
                    Nodes.Add(introducerManager.Find(u => u.UserID == _introducer.LeftID));
                }
                if (_introducer.RightID > 0)
                {
                    near++;
                    Nodes.Add(introducerManager.Find(u => u.UserID == _introducer.RightID));
                }
            }
            return nodelist;
        }
        public ActionResult Active()
        {
            return View("~/Views/Mobile/Zone/Active.cshtml");
        }
        public ActionResult ChangePassword()
        {
            return View("~/Views/Mobile/Zone/ChangePassword.cshtml");
        }
        [HttpPost]
        ///修改密码///
        public ActionResult ChangePassword(int Id, string Oldpass, string Newpass)
        {
            var _user = userManager.Find(Id);
            if (Security.SHA256(Oldpass) != _user.Password)
            {
                return Content("0");
            }
            if (Newpass == Oldpass)
            {
                return Content("1");
            }
            else
            {

                _user.Password = Security.SHA256(Newpass);
                userManager.Update(_user);
                return Content("2");
            }
        }
        /// <summary>
        /// 修改手机号
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePhone()
        {
            return View("~/Views/Mobile/Zone/ChangePhone.cshtml");
        }
        [HttpPost]
        public ActionResult ChangePhone(string Phone)
        {
            int Id = int.Parse(Request.Cookies["UserInfo"]["UserID"]);
            var _user = userManager.Find(Id);
            if (Phone == _user.Phone)
            {
                return Content("0");
            }
            else
            {
                _user.Phone = Phone;
                userManager.Update(_user);
                return Content("1");
            }

        }
        public ActionResult ChangeAccountCard()
        {
            return View("~/Views/Mobile/Zone/ChangeAccountCard.cshtml");
        }
        [HttpPost]
        public ActionResult ChangeAccountCard(string AccountCard)
        {
            int Id = int.Parse(Request.Cookies["UserInfo"]["UserID"]);
            var _user = userManager.Find(Id);
            if (AccountCard == _user.AccountCard)
            {
                return Content("0");
            }
            else
            {
                _user.AccountCard = AccountCard;
                userManager.Update(_user);
                return Content("1");
            }
        }
        public ActionResult ChangeSex()
        {
            return View("~/Views/Mobile/Zone/ChangeSex.cshtml");
        }
        [HttpPost]
        public ActionResult ChangeSex(int Sex)
        {
            int Id = int.Parse(Request.Cookies["UserInfo"]["UserID"]);
            var _user = userManager.Find(Id);
            _user.Sex = Sex;
            userManager.Update(_user);
            return Content("1");
        }
        public ActionResult Calendar()
        {
            return View("~/Views/Mobile/Zone/calendar.cshtml");
        }
        public ActionResult Center()
        {
            return View("~/Views/Mobile/Zone/Index.cshtml");
        }
        public ActionResult Withdrawal()
        {
            return View("~/Views/Mobile/Zone/Withdrawal.cshtml");
        }
    }
}